package com.tr.ssocms.shiro;


import com.tr.common.bo.Member;
import com.tr.ssocms.service.RemoteMemberService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class MemberRealm extends AuthorizingRealm {


    @Autowired
    private RemoteMemberService remoteMemberService;


    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = authenticationToken.getPrincipal().toString();
        System.out.println("用户名"+userName);
        Member realMember = remoteMemberService.queryByUsername(userName);
        if(realMember == null){
            throw new AccountException();
        }
        System.out.println(realMember);
        return new SimpleAuthenticationInfo(realMember,realMember.getPassword(),getName());
    }
}
